package jsu.blogger.service.impl;

import jsu.blogger.mapper.LiveMapper;
import jsu.blogger.pojo.Post;
import jsu.blogger.service.inter.LiveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class LiveServiceImpl implements LiveService {
    private static final Logger log = LoggerFactory.getLogger(LiveServiceImpl.class);
    @Autowired
    private LiveMapper liveMapper;
    @Override
    public Long insertOneLive(Long uid, Timestamp timestamp, String text) {
        Post post=new Post();
        post.setUserId(uid);
        post.setContent(text);
        post.setCreatedAt(timestamp);
        liveMapper.insertOne(post);
        return post.getPostId();
    }

    @Override
    public Post selectOneLive(Long postId) {
        return liveMapper.selectLiveBuPostId(postId);
    }

    @Override
    public List<Long> selectFollowedUid(Long uid) {
        return liveMapper.selectFollowedUsersByUid(uid);
    }

    @Override
    public List<Long> selectFollowersUid(Long uid) {
        return liveMapper.selectFollowersByUid(uid);
    }

    @Override
    public List<Post> selectLiveLimit(int offset, int limit) {
        return liveMapper.selectLiveAllLimit(offset,limit);
    }

    @Override
    public List<Post> selectLiveByUid(Long uid, int offset, int limit) {
        return liveMapper.selectLiveByUid(uid,offset,limit);
    }

    @Override
    public List<Post> selectLiveAll() {
        return liveMapper.selectLiveAll();
    }

    @Override
    public Timestamp isFollow(Long uid1, Long uid2) {
        Timestamp timestamp=null;
        try{
            timestamp=liveMapper.isUser1FollowUser2(uid1,uid2);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return timestamp;
    }

    @Override
    public boolean followSb(Long uid1, Long uid2,Timestamp timestamp) {
        return liveMapper.insertUserFollow(uid1, uid2, timestamp)==1?true:false;
    }

    @Override
    public boolean cancelFollowSb(Long uid1, Long uid2) {
        return liveMapper.deleteUserFollow(uid1, uid2)==1?true:false;
    }
    @Override
    public boolean deleteLiveByPostId(Long postId) {
        try {
            liveMapper.deleteLiveTagsByPostIdToDeletePost(postId);
            return liveMapper.deleteLiveByPostId(postId)==1?true:false;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean addLiveLike(Long postId,Long uid,int isLike,Timestamp createdAt) {//点赞帖子操作
            return (liveMapper.updatePostLikeAddOneInsert(postId,uid,isLike,createdAt)==1
                    &&liveMapper.updatePostLikeAddOneUpdate(postId)==1)?true:false;
    }
    @Override
    public Integer isPostUserLike(Long postId, Long uid){//检查用户是否给帖子点赞了
        return liveMapper.selectIsLikeByPostIdAndUid(postId,uid);
    }
    @Override
    public int getPostLikes(Long postId){
        return liveMapper.selectLikeCountByPostId(postId);
    }
    @Override
    public String getLiveContent(Long postId){
        return liveMapper.selectPostContentByPostId(postId);
    }
    @Override
    public boolean deleteLiveLike(Long postId, Long uid){
            return (liveMapper.deletePostLikeDelete(postId,uid)==1
                    &&liveMapper.deletePostLikeUpdate(postId,uid)==1)?true:false;
    }
    @Override
    public Long getUserIdByPostId(Long postId){
        return liveMapper.selectUserIdByPostId(postId);
    }
    @Override
    public List<Long> getPostIdByUserId(Long userId, int begin, int count){
        return liveMapper.selectPostIdByUserId(userId,begin,count);
    }
}
